package thread;

/**
 * 死锁
 * 死锁是指两个(或多个)线程间各自持有一个锁的同时等待对方先释放锁，这会形成一种僵持状态，这个现象称为死锁。
 */
public class DeadLockDemo {
    public static Object chopstick = new Object();//筷子
    public static Object spoon = new Object();//勺

    public static void main(String[] args) {
        //北方人
        Thread np = new Thread(){
            public void run(){
                try {
                    System.out.println("北方人开始吃饭");
                    System.out.println("北方人去拿筷子...");
                    synchronized (chopstick){
                        System.out.println("北方人拿起了筷子，开始吃饭...");
                        Thread.sleep(5000);//吃饭过程...
                        System.out.println("北方人去拿勺...");
                        synchronized (spoon){
                            System.out.println("北方人拿起了勺，开始喝汤...");
                            Thread.sleep(5000);//喝汤过程...
                        }
                        System.out.println("北方人喝完了汤并放下了勺");
                    }
                    System.out.println("北方人吃完了饭并放下了筷子");
                } catch (InterruptedException e) {
                }
            }
        };

        //南方人
        Thread sp = new Thread(){
            public void run(){
                try {
                    System.out.println("南方人开始吃饭");
                    System.out.println("南方人去拿勺...");
                    synchronized (spoon){
                        System.out.println("南方人拿起了勺，开始喝汤...");
                        Thread.sleep(5000);//喝汤过程...
                        System.out.println("南方人去拿筷子...");
                        synchronized (chopstick){
                            System.out.println("南方人拿起了筷子，开始吃饭...");
                            Thread.sleep(5000);//吃饭过程...
                        }
                        System.out.println("南方人吃完了饭并放下了筷子");
                    }
                    System.out.println("南方人喝完了汤并放下了勺");
                } catch (InterruptedException e) {
                }
            }
        };
        sp.start();
        np.start();
    }
}
